package com.beta.cust.bind.datasync.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beta.cust.bind.datasync.constant.BusinessConstant;
import com.beta.cust.bind.datasync.mapper.entity.EmpCustRef;
import com.beta.cust.bind.datasync.mapper.EmpCustRefMapper;
import com.beta.cust.bind.datasync.service.IEmpCustRefService;
import com.beta.cust.bind.datasync.utils.CollectionUtil;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工-客户关联表 服务实现类
 * </p>
 *
 * @author pcc
 * @since 2024-03-12
 */
@Service
public class EmpCustRefServiceImpl extends ServiceImpl<EmpCustRefMapper, EmpCustRef> implements IEmpCustRefService {

    @Override
    public List<EmpCustRef> selectExistList(String userId, List<String> externalUseridList) {
        if(StringUtil.isEmpty(userId) || CollectionUtils.isEmpty(externalUseridList)){
            return new ArrayList<>();
        }
        return this.baseMapper.selectList(new LambdaQueryWrapper<EmpCustRef>()
                .eq(EmpCustRef::getDelFlag, BusinessConstant.IS_NOT_DEL_FLAG)
                .eq(EmpCustRef::getUserId, userId)
                .in(EmpCustRef::getExternalUserid, externalUseridList));
    }

    @Override
    public void updateTimeBatchByList(List<EmpCustRef> empCustRefList) {
        if(CollectionUtils.isEmpty(empCustRefList)){
            return;
        }
        List<String> externalUserIdList = empCustRefList.stream().map(EmpCustRef::getExternalUserid).collect(Collectors.toList());
        EmpCustRef updateEntity = new EmpCustRef();
        updateEntity.setUpdateTime(empCustRefList.get(0).getUpdateTime());
        this.baseMapper.update(updateEntity,new LambdaQueryWrapper<EmpCustRef>()
                .eq(EmpCustRef::getDelFlag,BusinessConstant.IS_NOT_DEL_FLAG)
                .in(EmpCustRef::getExternalUserid,externalUserIdList));
    }
}
